var findMagicIndex = function(nums) {
    var left = 0, length = nums.length, right = nums[length-1], result = -1;
    if(nums[0]>length || nums[length-1]<0){
        return -1;
    }
    if(nums.some( (value,index) => {
        result = index;
        return value === index;
    })){
        return result;
    }
    return -1;
};

// 二分
var findMagicIndex2 = function(nums) {
    var left = 0, length = nums.length, right = length-1, result = -1;
    if(nums[0]>length || nums[length-1]<0){
        return -1;
    }
    while(left<=right){
        result = Math.floor((left+right)/2);
        if(nums[result]>result){ //二分处值大于当前索引，则后面的数据也一定大于，只考虑前半部分
            right = result-1;
        } else if(nums[reult]<result){
            left = result+1;
        } else{
            return result;
        }
    }
    return -1;
};


var num = [12296169, 14481887, 19365401, 71948694, 101256536, 137449705, 147615033, 169095970, 182939974, 183054331, 191033174, 200069688, 210281043, 211549396, 227193353, 252408327, 263757832, 268669870, 271916258, 293898012, 322628245, 329246885, 348479255, 405807814, 431800160, 449369511, 477566467, 481431749, 481880069, 487953610, 509211052, 520721303, 527744664, 550058864, 571603718, 571617555, 579098239, 582152388, 645340207, 681566032, 685774515, 706348591, 708774328, 717815831, 721421995, 724666698, 745560058, 746289154, 769651867, 781893631, 789714924, 807615645, 882508445, 884260053, 916797901, 920985226, 924045345, 932899253, 950715182, 987825772, 1015158842, 1016121780, 1065377233, 1072449577];
console.log(findMagicIndex2(num));